package com.example.myapplication.ui.execution;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.example.myapplication.bean.Execution;
import com.example.myapplication.service.ExecutionService;
import com.example.myapplication.ui.component.CustomToast;
import com.example.myapplication.util.StringUtils;

import java.util.Calendar;

public class ExecutionInput extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "ExecutionInput";
    private TextView dateShowArea;
    private TextView taskIdText;
    private TextView personNameText;
    private TextView contentText;
    private TextView remarkText;
    private ImageButton calendarBtn;
    private Dialog bottomDialog;
    private DatePicker datePicker;
    private LinearLayout commitBtn;
    private CustomToast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.work_execution_input);
        dateShowArea = findViewById(R.id.execution_date_show_area);
        taskIdText = findViewById(R.id.execution_taskId_text);
        personNameText = findViewById(R.id.execution_checkMan_text);
        contentText = findViewById(R.id.execution_work_content_text);
        remarkText = findViewById(R.id.execution_remark_text);
        calendarBtn = findViewById(R.id.execution_calendar_btn);
        commitBtn = findViewById(R.id.commit_execution);
        calendarBtn.setOnClickListener(this);
        commitBtn.setOnClickListener(this);
        initDateShowArea();
        personNameText.setText(getIntent().getStringExtra("name"));
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.execution_calendar_btn:
                showBottomDialog();
                loadDatePicker();
                setDatePickerListenerAndGetInfo();
                break;
            case R.id.commit_execution:
                openCommitDialog();
                break;
        }
    }

    private void showBottomDialog() {
        bottomDialog = new Dialog(this, R.style.BottomDialog);
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_date_picker, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.width = getResources().getDisplayMetrics().widthPixels;
        contentView.setLayoutParams(layoutParams);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottomDialog.show();
    }

    private void loadDatePicker() {
        datePicker = bottomDialog.findViewById(R.id.date_picker_inspection_check);
    }

    public void setDatePickerListenerAndGetInfo() {
        int year = 0;
        int month = 0;
        int day = 0;

        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DATE);
        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//                Log.d(TAG, "onDateChanged: "+year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
                setDateShowArea(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
            }
        });
    }

    public void initDateShowArea() {
        int year = 0;
        int month = 0;
        int day = 0;
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DATE);
        setDateShowArea(year + "-" + month + "-" + day);
    }

    public void setDateShowArea(String pickerDate) {
        dateShowArea.setText(pickerDate);
    }

    public void openCommitDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(ExecutionInput.this);
        builder.setTitle("提交确认");
        builder.setIcon(R.drawable.ic_commit);//设置标题图
        builder.setMessage("\n" + "   确定提交？");
        builder.setPositiveButton("确定", (arg0, arg1) -> commitExecution());
        builder.setNegativeButton("再改改", (arg0, arg1) -> Toast.makeText(ExecutionInput.this, "cancel", Toast.LENGTH_SHORT).show());
        AlertDialog dialog = builder.create();
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setBackgroundColor(Color.WHITE);
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE);
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setBackgroundColor(Color.WHITE);
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
    }

    private Integer commitExecution() {
        if (isInputEmpty()) {
             toastMessage("输入的信息项不能为空！");
            return null;
        }
        Integer status;
        status= new ExecutionService().addExecution(
                new Execution(dateShowArea.getText().toString(),
                        Integer.parseInt(taskIdText.getText().toString()),
                        personNameText.getText().toString(),
                        contentText.getText().toString(),
                        remarkText.getText().toString()));
        if (status==200){
            toastMessage("添加成功！");
        }
        else{
            toastMessage("添加失败，已存在任务编号");
        }
        return status;
    }

    public boolean isInputEmpty() {
        String dataTextInfo = dateShowArea.getText().toString();
        String taskIdInfo = taskIdText.getText().toString();
        String personName = personNameText.getText().toString();
        String content = contentText.getText().toString();
        String remark = remarkText.getText().toString();
        if (StringUtils.isEmpty(dataTextInfo) || StringUtils.isEmpty(taskIdInfo)
                || StringUtils.isEmpty(personName) || StringUtils.isEmpty(content) || StringUtils.isEmpty(remark)) {
            return true;
        }
        return false;
    }

    private void toastMessage(String content) {
        if (toast != null) {
            toast.hide();
        }
        toast = new CustomToast(ExecutionInput.this,
                (ViewGroup) this.findViewById(R.id.toast_custom_parent));
        toast.show(content, 1000);
    }
}
